#include "MenuScene.h"
#include "Helpers/Helpers.h"
#include "StarComponents.h"
#include "Input/InputManager.h"
#include "Graphics/GraphicsManager.h"
#include  "Graphics/SpriteAnimationManager.h"
#include "Scenes/SceneManager.h"
#include "Sound/SoundService.h"
#include "Assets/FontManager.h"
#include "../Objects/GlobalVariables.h"
#include "MainScene.h"

#ifdef DESKTOP
#include <glew.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif

#define INPUT_MANAGER (star::InputManager::GetInstance())
#define LOGGER (star::Logger::GetInstance())

MenuScene::MenuScene(const tstring & Name):
	BaseScene(Name),
	m_pActiveCamera(nullptr),
	m_pBackground(nullptr),
	m_pBlock1(nullptr),
	m_pBlock2(nullptr),
	m_pBlock3(nullptr),
	m_pBlock4(nullptr),
	m_pLine1(nullptr),
	m_pLine2(nullptr),
	m_pLine3(nullptr),
	m_pLine4(nullptr),
	m_pDeletionBlock1(nullptr),
	m_pDeletionBlock2(nullptr),
	m_pDeletionBlock3(nullptr),
	m_pDeletionBlock4(nullptr),
	m_pScreenOverlay(nullptr),
	m_pLoadingScreen(nullptr),
	m_pButton(nullptr),
	m_PassedMiliseconds(0.0f),
	m_bFirstTime(true),
	m_bThatOneTick(true)
{
		
}

MenuScene::~MenuScene()
{

}

void MenuScene::CreateObjects()
{
	m_pActiveCamera = new star::BaseCamera();
	AddObject(m_pActiveCamera);
	
	//Spritesheet loading
	star::SpriteAnimationManager::GetInstance()->AddSpritesheet(_T("Tiles/TileAnimation.xml"),_T("Tiles/TileAnimation.star"));
	star::SpriteAnimationManager::GetInstance()->AddSpritesheet(_T("Tiles/TileMenuAnimation.xml"), _T("Tiles/TileMenuAnimation.star"));
	star::SpriteAnimationManager::GetInstance()->AddSpritesheet(_T("Tiles/LineAnimation.xml"),_T("Tiles/LineAnimation.star"));

	//background
	m_pBackground = new star::Object();
	m_pBackground->AddComponent(new star::SpriteComponent(_T("Menu/Background.png"), _T("Background")));
	AddObject(m_pBackground);

	//button
	m_pButton = new StartButton();
	m_pButton->GetTransform()->Translate(9, 35);
	AddObject(m_pButton);

	//All animation objects
	m_pBlock1 = new star::Object();
	m_pBlock1->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("TilesMenu")));
	AddObject(m_pBlock1);
	m_pBlock1->GetTransform()->Translate(88,44);
	
	m_pBlock2 = new star::Object();
	m_pBlock2->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("TilesMenu")));
	AddObject(m_pBlock2);
	m_pBlock2->GetTransform()->Translate(98,44);

	m_pBlock3 = new star::Object();
	m_pBlock3->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("TilesMenu")));
	AddObject(m_pBlock3);
	m_pBlock3->GetTransform()->Translate(88,34);

	m_pBlock4 = new star::Object();
	m_pBlock4->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("TilesMenu")));
	AddObject(m_pBlock4);
	m_pBlock4->GetTransform()->Translate(98,34);

	m_pLine1 = new star::Object();
	m_pLine1->AddComponent(new star::SpritesheetComponent(_T("Tiles/Line1.png"),_T("LineAnim"),_T("Line")));
	AddObject(m_pLine1);
	m_pLine1->GetTransform()->Translate(112,42);
	
	m_pLine2 = new star::Object();
	m_pLine2->AddComponent(new star::SpritesheetComponent(_T("Tiles/Line1.png"),_T("LineAnim"),_T("Line")));
	AddObject(m_pLine2);
	m_pLine2->GetTransform()->Translate(116,42);
	
	m_pLine3 = new star::Object();
	m_pLine3->AddComponent(new star::SpritesheetComponent(_T("Tiles/Line1.png"),_T("LineAnim"),_T("Line")));
	AddObject(m_pLine3); 
	m_pLine3->GetTransform()->Translate(120,42);
	
	m_pLine4 = new star::Object();
	m_pLine4->AddComponent(new star::SpritesheetComponent(_T("Tiles/Line1.png"),_T("LineAnim"),_T("Line")));
	AddObject(m_pLine4);
	m_pLine4->GetTransform()->Translate(124,42);
	
	m_pDeletionBlock1 = new star::Object();
	m_pDeletionBlock1->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("Tiles")));
	AddObject(m_pDeletionBlock1);
	m_pDeletionBlock1->GetTransform()->Translate(131,44);
	
	m_pDeletionBlock2 = new star::Object();
	m_pDeletionBlock2->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("Tiles")));
	AddObject(m_pDeletionBlock2);
	m_pDeletionBlock2->GetTransform()->Translate(141,44);
	
	m_pDeletionBlock3 = new star::Object();
	m_pDeletionBlock3->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("Tiles")));
	AddObject(m_pDeletionBlock3);
	m_pDeletionBlock3->GetTransform()->Translate(131,34);

	m_pDeletionBlock4 = new star::Object();
	m_pDeletionBlock4->AddComponent(new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("Tiles")));
	AddObject(m_pDeletionBlock4);
	m_pDeletionBlock4->GetTransform()->Translate(141,34);

	m_pHighScore = new star::Object();
	m_pHighScore->AddComponent(new star::TextComponent(_T("PixelMotion.ttf"),_T("Score"), 40));
	AddObject(m_pHighScore);
	m_pHighScore->GetTransform()->Translate(55, 14);
    m_pHighScore->GetTransform()->Scale(0.15f);

	m_pLoadingScreen = new PauseScreen(true);
	AddObject(m_pLoadingScreen);
	m_pLoadingScreen->SetDisabled(true);

	m_pScreenOverlay = new star::Object();
	m_pScreenOverlay->AddComponent(new star::SpriteComponent(_T("HUD/ScreenOverlay.png"),_T("Overlay"), true, true));
	AddObject(m_pScreenOverlay);

	star::SoundService::GetInstance()->LoadMusic(_T("Sounds/GameJoy.ogg"),_T("GameJoy"));
}

void MenuScene::AfterInitializedObjects()
{
	BaseScene::AfterInitializedObjects();
	star::SoundService::GetInstance()->PlaySoundFile(_T("GameJoy"),-1);
	
	m_pDeletionBlock1->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("TopLeft_Black"));
	m_pDeletionBlock2->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("TopRight_Black"));
	m_pDeletionBlock3->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("BottomLeft_Black"));
	m_pDeletionBlock4->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("BottomRight_Black"));
}

void MenuScene::Update(const star::Context& context)
{
	SetActiveCamera(m_pActiveCamera);

	tstringstream score;
	score << GlobalVariables::GetInstance()->GetHighScore();
	m_pHighScore->GetComponent<star::TextComponent>()->SetText(score.str());

	if(!m_bThatOneTick)
	{
		m_pLoadingScreen->SetVisible(true);
	}

#ifdef DESKTOP
	if((INPUT_MANAGER->IsKeyboardKeyDown('W', true) &&
		!INPUT_MANAGER->IsKeyboardKeyDown('W', false)) ||
		(INPUT_MANAGER->IsKeyboardKeyDown('Z', true) &&
		!INPUT_MANAGER->IsKeyboardKeyDown('Z', false))||
		(INPUT_MANAGER->IsKeyboardKeyDown('X', true) &&
		!INPUT_MANAGER->IsKeyboardKeyDown('X', false))||
		(INPUT_MANAGER->IsKeyboardKeyDown(VK_RETURN, true) &&
		!INPUT_MANAGER->IsKeyboardKeyDown(VK_RETURN, false)))
	{
		m_pLoadingScreen->SetDisabled(false);
		if(m_bThatOneTick)
		{
			m_pLoadingScreen->SetVisible(false);
			m_bThatOneTick = false;
		}
	}
#endif

	m_PassedMiliseconds += float(context.mTimeManager->GetSeconds());
	if(m_PassedMiliseconds >= 1.2f)
	{
		m_pDeletionBlock1->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("TopLeft_Black"));
		m_pDeletionBlock2->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("TopRight_Black"));
		m_pDeletionBlock3->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("BottomLeft_Black"));
		m_pDeletionBlock4->GetComponent<star::SpritesheetComponent>()->PlayAnimation(_T("BottomRight_Black"));
		m_PassedMiliseconds = 0;
	}

	if(m_pLoadingScreen->IsFinished())
	{	
		if(!m_bFirstTime)
		{
			MainScene* mainScene = reinterpret_cast<MainScene*>(star::SceneManager::GetInstance()->GetScene(_T("MainScene")));
			mainScene->ResetGame();
		}
		m_bFirstTime = false;
		star::SceneManager::GetInstance()->SetActiveScene(_T("MainScene"));
	}
}

void MenuScene::Reset()
{
	m_pLoadingScreen->Reset();
	m_bThatOneTick = true;
}


